package registnet.dao;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Vector;

import org.apache.torque.TorqueException;
import org.apache.torque.util.Criteria;

import registnet.tokenizer.RegistTokenizer;
import resources.Constants;

public class CurriculumPeer extends registnet.dao.BaseCurriculumPeer {
	String logUnitCode = new String();
	String user = new String();
	
	/**
	 * Default constructor
	 */
	public CurriculumPeer(){}
	
	
	/**
	 * Inserts a new curriculum
	 * 
	 * @param curriculumCode - The code assigned to a curriculum
	 * @param curriculumName - The name of the curriculum
	 * @param degreeId - The autogenerated id of the degree course
	 * @return	The autogenerated id of the curriculum
	 */
	public Integer createNewCurriculum(String curriculumCode, String curriculumName, Integer degreeId){
		Integer id = null;
		Criteria criteria = new Criteria();
		criteria.add(CurriculumPeer.CURRICULUMCODE, curriculumCode);
		criteria.and(CurriculumPeer.CURRICULUMNAME, curriculumName);
		criteria.and(CurriculumPeer.DEGREEID, degreeId);

		try{
			id = Integer.parseInt(CurriculumPeer.doInsert(criteria).toString());
			java.util.Calendar cal = Calendar.getInstance();
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm:ss");
			
			String dateFormat = sdf.format(cal.getTime());
			String[] date = dateFormat.split("-");
			
			String timeFormat = sdf1.format(cal.getTime());
			String[] time = timeFormat.split(":");
			String degreeCode = (new DegreeCoursePeer()).getDegreeCourse(degreeId).getDegreeCode();
			CurriculumLogPeer.createNewCurriculumLog(1, Integer.parseInt(date[0]), Integer.parseInt(date[1]), Integer.parseInt(date[2]) , Integer.parseInt(time[0]), Integer.parseInt(time[1]), Integer.parseInt(time[2]), curriculumCode, curriculumName, 0, 0, 0, 0, 0, degreeCode, this.user, this.logUnitCode);
		}catch (TorqueException e) {
			e.printStackTrace();
		}
		return id;
	}
	
	public Integer createRollbackCurriculum(String curriculumCode, String curriculumName, Integer totalUnits, Integer numberAH, Integer numberMST, Integer numberSSP, Integer numberElective, Integer degreeId){
		Integer id = null;
		Criteria criteria = new Criteria();
		criteria.add(CurriculumPeer.CURRICULUMCODE, curriculumCode);
		criteria.and(CurriculumPeer.CURRICULUMNAME, curriculumName);
		criteria.and(CurriculumPeer.TOTALUNITS, totalUnits);
		criteria.and(CurriculumPeer.NUMBERAH, numberAH);
		criteria.and(CurriculumPeer.NUMBERMST, numberMST);
		criteria.and(CurriculumPeer.NUMBERSSP, numberSSP);
		criteria.and(CurriculumPeer.NUMBERELECTIVE, numberElective);
		criteria.and(CurriculumPeer.DEGREEID, degreeId);

		try{
			Integer.parseInt(CurriculumPeer.doInsert(criteria).toString());
			
			java.util.Calendar cal = Calendar.getInstance();
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm:ss");
			
			String dateFormat = sdf.format(cal.getTime());
			String[] date = dateFormat.split("-");
			
			String timeFormat = sdf1.format(cal.getTime());
			String[] time = timeFormat.split(":");
			String degreeCode = (new DegreeCoursePeer()).getDegreeCourse(degreeId).getDegreeCode();
			
			id = CurriculumLogPeer.createNewCurriculumLog(1, Integer.parseInt(date[0]), Integer.parseInt(date[1]), Integer.parseInt(date[2]) , Integer.parseInt(time[0]), Integer.parseInt(time[1]), Integer.parseInt(time[2]), curriculumCode, curriculumName, totalUnits, numberAH, numberMST, numberSSP, numberElective, degreeCode, this.user, this.logUnitCode);
		}catch (TorqueException e) {
			e.printStackTrace();
		}
		return id;
	}
	
	
	/**
	 * Gets the autogenerated id of the curriculum
	 * 
	 * @param curriculumCode - The code assigned to the curriculum
	 * @return	The autogenerated id of the curriculum
	 */
	public Integer getCurriculumId(String curriculumCode){
		Criteria criteria = new Criteria();
		criteria.add(CurriculumPeer.CURRICULUMCODE, curriculumCode);
		List list;
		try{
			list = CurriculumPeer.doSelect(criteria);
			Curriculum curriculum = (Curriculum)list.get(0);
			return curriculum.getCurriculumId();
		}catch (Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	
	/**
	 * Gets the curriculum with the given id
	 * 
	 * @param curriculumId - The autogenerated id of the curriculum
	 * @return	The curriculum with the given id
	 */
	public Curriculum getCurriculum(Integer curriculumId){
		Criteria criteria = new Criteria();
		criteria.add(CurriculumPeer.CURRICULUMID, curriculumId);
		List list;
		try{
			list = CurriculumPeer.doSelect(criteria);
			return (Curriculum)list.get(0);
		}catch (Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	
	/**
	 * Gets the curriculum with the given curriculum code
	 * 
	 * @param curriculumCode - The code assigned to a curriculum
	 * @return	The curriculum with the given curriculum code
	 */
	public Curriculum getCurriculum(String curriculumCode){
		Criteria criteria = new Criteria();
		criteria.add(CurriculumPeer.CURRICULUMCODE, curriculumCode);
		try{
			return (Curriculum)CurriculumPeer.doSelect(criteria).get(0);
		}catch (Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	
	/**
	 * Updates the curriculum
	 * 
	 * @param curriculumId - The autogenerated id of the curriculum
	 * @param curriculumCode - The code assigned to the course
	 * @param curriculumName - Tha name of the curriculum
	 * @param totalUnits - The total number of units
	 * @param numberAH - The number of GE(AH) courses
	 * @param numberMST - The number of GE(MST) courses
	 * @param numberSSP - The number of GE(SSP) courses
	 * @param numberElective - The number of ELECTIVE courses
	 * @param degreeId - The autogenerated id of the degree course
	 * @return	true if the update is successful, false otherwise
	 */
	public boolean updateCurriculum(Integer curriculumId, String curriculumCode, String curriculumName, Integer totalUnits, Integer numberAH, Integer numberMST, Integer numberSSP, Integer numberElective, Integer degreeId){
		Criteria criteria = new Criteria();
		criteria.add(CurriculumPeer.CURRICULUMID, curriculumId);
		criteria.and(CurriculumPeer.CURRICULUMCODE, curriculumCode);
		criteria.and(CurriculumPeer.CURRICULUMNAME, curriculumName);
		criteria.and(CurriculumPeer.TOTALUNITS, totalUnits);
		criteria.and(CurriculumPeer.NUMBERAH, numberAH);
		criteria.and(CurriculumPeer.NUMBERMST, numberMST);
		criteria.and(CurriculumPeer.NUMBERSSP, numberSSP);
		criteria.and(CurriculumPeer.NUMBERELECTIVE, numberElective);
		criteria.and(CurriculumPeer.DEGREEID, degreeId);
		try{
			Criteria c = new Criteria();
			c.add(CurriculumPeer.CURRICULUMID, curriculumId);
			Curriculum curriculum = (Curriculum) CurriculumPeer.doSelect(c).get(0);
			Calendar cal = Calendar.getInstance();
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm:ss");
			
			String dateFormat = sdf.format(cal.getTime());
			String[] date = dateFormat.split("-");
			
			String timeFormat = sdf1.format(cal.getTime());
			String[] time = timeFormat.split(":");
			String degreeCode = (new DegreeCoursePeer()).getDegreeCourse(curriculum.getDegreeId()).getDegreeCode();
			String degreeCode1 = (new DegreeCoursePeer()).getDegreeCourse(degreeId).getDegreeCode();

			CurriculumPeer.doUpdate(criteria);
			CurriculumLogPeer.createNewCurriculumLog(2, Integer.parseInt(date[0]), Integer.parseInt(date[1]), Integer.parseInt(date[2]) , Integer.parseInt(time[0]), Integer.parseInt(time[1]), Integer.parseInt(time[2]), curriculum.getCurriculumCode(), curriculum.getCurriculumName(), curriculum.getTotalUnits(), curriculum.getAHCount(), curriculum.getMSTCount(), curriculum.getSSPCount(), curriculum.getElectiveCount(), degreeCode, this.user, this.logUnitCode);
			CurriculumLogPeer.createNewCurriculumLog(4, Integer.parseInt(date[0]), Integer.parseInt(date[1]), Integer.parseInt(date[2]) , Integer.parseInt(time[0]), Integer.parseInt(time[1]), Integer.parseInt(time[2]), curriculumCode, curriculumName, totalUnits, numberAH, numberMST, numberSSP, numberElective, degreeCode1, this.user, this.logUnitCode);
			return true;
		}catch (TorqueException e){
			e.printStackTrace();
			return false;
		}
	}
	
	
	/**
	 * Updates the curriculum
	 * 
	 * @param curriculumId - The autogenerated id of the curriculum
	 * @param curriculumCode - The code assigned to the course
	 * @param curriculumName - Tha name of the curriculum
	 * @param degreeId - The autogenerated id of the degree course
	 * @return	true if the update is successful, false otherwise
	 */
	public boolean updateCurriculum(Integer curriculumId, String curriculumCode, String curriculumName, Integer degreeId){
		Criteria criteria = new Criteria();
		criteria.add(CurriculumPeer.CURRICULUMID, curriculumId);
		criteria.and(CurriculumPeer.CURRICULUMCODE, curriculumCode);
		criteria.and(CurriculumPeer.CURRICULUMNAME, curriculumName);
		criteria.and(CurriculumPeer.DEGREEID, degreeId);
		try{
			Criteria c = new Criteria();
			c.add(CurriculumPeer.CURRICULUMID, curriculumId);
			Curriculum curriculum = (Curriculum) CurriculumPeer.doSelect(c).get(0);
			Calendar cal = Calendar.getInstance();
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm:ss");
			
			String dateFormat = sdf.format(cal.getTime());
			String[] date = dateFormat.split("-");
			
			String timeFormat = sdf1.format(cal.getTime());
			String[] time = timeFormat.split(":");
			String degreeCode = (new DegreeCoursePeer()).getDegreeCourse(curriculum.getDegreeId()).getDegreeCode();
			String degreeCode1 = (new DegreeCoursePeer()).getDegreeCourse(degreeId).getDegreeCode();

			CurriculumPeer.doUpdate(criteria);
			CurriculumLogPeer.createNewCurriculumLog(2, Integer.parseInt(date[0]), Integer.parseInt(date[1]), Integer.parseInt(date[2]) , Integer.parseInt(time[0]), Integer.parseInt(time[1]), Integer.parseInt(time[2]), curriculum.getCurriculumCode(), curriculum.getCurriculumName(), curriculum.getTotalUnits(), curriculum.getAHCount(), curriculum.getMSTCount(), curriculum.getSSPCount(), curriculum.getElectiveCount(), degreeCode, this.user, this.logUnitCode);
			CurriculumLogPeer.createNewCurriculumLog(4, Integer.parseInt(date[0]), Integer.parseInt(date[1]), Integer.parseInt(date[2]) , Integer.parseInt(time[0]), Integer.parseInt(time[1]), Integer.parseInt(time[2]), curriculumCode, curriculumName, curriculum.getTotalUnits(), curriculum.getAHCount(), curriculum.getMSTCount(), curriculum.getSSPCount(), curriculum.getElectiveCount(), degreeCode1, this.user, this.logUnitCode);
			return true;
		}catch (TorqueException e){
			e.printStackTrace();
			return false;
		}
	}
	
	
	/**
	 * Updates the curriculum
	 * 
	 * @param curriculumId - The autogenerated id of the curriculum
	 * @param totalUnits - The total number of units
	 * @param numberAH - The number of GE(AH) courses
	 * @param numberMST - The number of GE(MST) courses
	 * @param numberSSP - The number of GE(SSP) courses
	 * @param numberElective - The number of ELECTIVE courses
	 * @return	true if the update is successful, false otherwise
	 */
	public boolean updateCurriculum(Integer curriculumId, Integer totalUnits, Integer numberAH, Integer numberMST, Integer numberSSP, Integer numberElective){
		Criteria criteria = new Criteria();
		criteria.add(CurriculumPeer.CURRICULUMID, curriculumId);
		criteria.and(CurriculumPeer.TOTALUNITS, totalUnits);
		criteria.and(CurriculumPeer.NUMBERAH, numberAH);
		criteria.and(CurriculumPeer.NUMBERMST, numberMST);
		criteria.and(CurriculumPeer.NUMBERSSP, numberSSP);
		criteria.and(CurriculumPeer.NUMBERELECTIVE, numberElective);
		try{
			Criteria c = new Criteria();
			c.add(CurriculumPeer.CURRICULUMID, curriculumId);
			Curriculum curriculum = (Curriculum) CurriculumPeer.doSelect(c).get(0);
			Calendar cal = Calendar.getInstance();
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm:ss");
			
			String dateFormat = sdf.format(cal.getTime());
			String[] date = dateFormat.split("-");
			
			String timeFormat = sdf1.format(cal.getTime());
			String[] time = timeFormat.split(":");
			String degreeCode = (new DegreeCoursePeer()).getDegreeCourse(curriculum.getDegreeId()).getDegreeCode();

			CurriculumPeer.doUpdate(criteria);
			CurriculumLogPeer.createNewCurriculumLog(2, Integer.parseInt(date[0]), Integer.parseInt(date[1]), Integer.parseInt(date[2]) , Integer.parseInt(time[0]), Integer.parseInt(time[1]), Integer.parseInt(time[2]), curriculum.getCurriculumCode(), curriculum.getCurriculumName(), curriculum.getTotalUnits(), curriculum.getAHCount(), curriculum.getMSTCount(), curriculum.getSSPCount(), curriculum.getElectiveCount(), degreeCode, this.user, this.logUnitCode);
			CurriculumLogPeer.createNewCurriculumLog(4, Integer.parseInt(date[0]), Integer.parseInt(date[1]), Integer.parseInt(date[2]) , Integer.parseInt(time[0]), Integer.parseInt(time[1]), Integer.parseInt(time[2]), curriculum.getCurriculumCode(), curriculum.getCurriculumName(), totalUnits, numberAH, numberMST, numberSSP, numberElective, degreeCode, this.user, this.logUnitCode);
			
			return true;
		}catch (TorqueException e){
			e.printStackTrace();
			return false;
		}
	}
	
	public Integer updateRollbackCurriculum(Integer curriculumId, String curriculumCode, String curriculumName, Integer totalUnits, Integer numberAH, Integer numberMST, Integer numberSSP, Integer numberElective, Integer degreeId){
		Integer id=null;
		Criteria criteria = new Criteria();
		criteria.add(CurriculumPeer.CURRICULUMID, curriculumId);
		criteria.and(CurriculumPeer.CURRICULUMCODE, curriculumCode);
		criteria.and(CurriculumPeer.CURRICULUMNAME, curriculumName);
		criteria.and(CurriculumPeer.TOTALUNITS, totalUnits);
		criteria.and(CurriculumPeer.NUMBERAH, numberAH);
		criteria.and(CurriculumPeer.NUMBERMST, numberMST);
		criteria.and(CurriculumPeer.NUMBERSSP, numberSSP);
		criteria.and(CurriculumPeer.NUMBERELECTIVE, numberElective);
		criteria.and(CurriculumPeer.DEGREEID, degreeId);
		try{
			Criteria c = new Criteria();
			c.add(CurriculumPeer.CURRICULUMID, curriculumId);
			Curriculum curriculum = (Curriculum) CurriculumPeer.doSelect(c).get(0);
			Calendar cal = Calendar.getInstance();
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm:ss");
			
			String dateFormat = sdf.format(cal.getTime());
			String[] date = dateFormat.split("-");
			
			String timeFormat = sdf1.format(cal.getTime());
			String[] time = timeFormat.split(":");
			String degreeCode = (new DegreeCoursePeer()).getDegreeCourse(curriculum.getDegreeId()).getDegreeCode();
			String degreeCode1 = (new DegreeCoursePeer()).getDegreeCourse(degreeId).getDegreeCode();

			CurriculumPeer.doUpdate(criteria);
			id = CurriculumLogPeer.createNewCurriculumLog(2, Integer.parseInt(date[0]), Integer.parseInt(date[1]), Integer.parseInt(date[2]) , Integer.parseInt(time[0]), Integer.parseInt(time[1]), Integer.parseInt(time[2]), curriculum.getCurriculumCode(), curriculum.getCurriculumName(), curriculum.getTotalUnits(), curriculum.getAHCount(), curriculum.getMSTCount(), curriculum.getSSPCount(), curriculum.getElectiveCount(), degreeCode, this.user, this.logUnitCode);
			CurriculumLogPeer.createNewCurriculumLog(4, Integer.parseInt(date[0]), Integer.parseInt(date[1]), Integer.parseInt(date[2]) , Integer.parseInt(time[0]), Integer.parseInt(time[1]), Integer.parseInt(time[2]), curriculumCode, curriculumName, totalUnits, numberAH, numberMST, numberSSP, numberElective, degreeCode1, this.user, this.logUnitCode);
			return id;
		}catch (TorqueException e){
			e.printStackTrace();
			return null;
		}
	}
	
	
	/**
	 * Deletes the curriculum with the given id
	 * 
	 * @param curriculumId - The autogenerated id of the curriculum
	 * @return	true if the delete is successful, false otherwise
	 */
	public boolean deleteCurriculum(Integer curriculumId){
		CurriculumCoursePeer ccp = new CurriculumCoursePeer();
		List<CurriculumCourse> temp = new CurriculumCoursePeer().getCurriculumCourses(curriculumId);
		ListIterator<CurriculumCourse> li = temp.listIterator();
		while(li.hasNext()){
			ccp.deleteCurriculumCourse(li.next());
		}
		
		Criteria criteria = new Criteria();
		criteria.add(CurriculumPeer.CURRICULUMID, curriculumId);
		try{
			Curriculum curriculum = getCurriculum(curriculumId);
			CurriculumPeer.doDelete(criteria);
			java.util.Calendar cal = Calendar.getInstance();
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm:ss");
			
			String dateFormat = sdf.format(cal.getTime());
			String[] date = dateFormat.split("-");
			
			String timeFormat = sdf1.format(cal.getTime());
			String[] time = timeFormat.split(":");
			String degreeCode = (new DegreeCoursePeer()).getDegreeCourse(curriculum.getDegreeId()).getDegreeCode();
			
			CurriculumLogPeer.createNewCurriculumLog(3, Integer.parseInt(date[0]), Integer.parseInt(date[1]), Integer.parseInt(date[2]) , Integer.parseInt(time[0]), Integer.parseInt(time[1]), Integer.parseInt(time[2]), curriculum.getCurriculumCode(), curriculum.getCurriculumName(), curriculum.getTotalUnits(), curriculum.getAHCount(), curriculum.getMSTCount(), curriculum.getSSPCount(), curriculum.getElectiveCount(), degreeCode, this.user, this.logUnitCode);
			return true;
		}catch (TorqueException e){
			e.printStackTrace();
			return false;
		}
	}
	
	public Integer deleteRollbackCurriculum(Integer curriculumId){
		Integer id=null;
		CurriculumCoursePeer ccp = new CurriculumCoursePeer();
		List<CurriculumCourse> temp = new CurriculumCoursePeer().getCurriculumCourses(curriculumId);
		ListIterator<CurriculumCourse> li = temp.listIterator();
		while(li.hasNext()){
			ccp.deleteCurriculumCourse(li.next());
		}
		
		Criteria criteria = new Criteria();
		criteria.add(CurriculumPeer.CURRICULUMID, curriculumId);
		try{
			Curriculum curriculum = getCurriculum(curriculumId);
			CurriculumPeer.doDelete(criteria);
			java.util.Calendar cal = Calendar.getInstance();
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm:ss");
			
			String dateFormat = sdf.format(cal.getTime());
			String[] date = dateFormat.split("-");
			
			String timeFormat = sdf1.format(cal.getTime());
			String[] time = timeFormat.split(":");
			String degreeCode = (new DegreeCoursePeer()).getDegreeCourse(curriculum.getDegreeId()).getDegreeCode();
			
			id = CurriculumLogPeer.createNewCurriculumLog(3, Integer.parseInt(date[0]), Integer.parseInt(date[1]), Integer.parseInt(date[2]) , Integer.parseInt(time[0]), Integer.parseInt(time[1]), Integer.parseInt(time[2]), curriculum.getCurriculumCode(), curriculum.getCurriculumName(), curriculum.getTotalUnits(), curriculum.getAHCount(), curriculum.getMSTCount(), curriculum.getSSPCount(), curriculum.getElectiveCount(), degreeCode, this.user, this.logUnitCode);
			return id;
		}catch (TorqueException e){
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * Gets the curricula associated with the given degree
	 * 
	 * @param degreeId - The autogenerated id of the degree course 
	 * @return	The curricula associated with the given degree
	 */
	public List<Curriculum> getCurriculumByDegreeCourse(Integer degreeId){
		Criteria criteria = new Criteria();
		criteria.add(CurriculumPeer.DEGREEID, degreeId);
		criteria.addAscendingOrderByColumn(CURRICULUMCODE);
		List temp;
		try{
			ArrayList<Curriculum> list = new ArrayList<Curriculum>();
			temp = CurriculumPeer.doSelect(criteria);
			ListIterator li = temp.listIterator();
			while(li.hasNext()){
				list.add(((Curriculum)li.next()));
			}
			return list;
		}catch (TorqueException e){
			e.printStackTrace();
			return null;
		}
	}
	
	
	/**
	 * Gets the curricula offered by the given college
	 * 
	 * @param collegeId - The autogenerated id of the college
	 * @return	The curricula offered by the given college
	 */
	public List<Curriculum> getCurriculumByCollege(Integer collegeId){
		Criteria criteria = new Criteria();
		criteria.addJoin(CurriculumPeer.DEGREEID, DegreeCoursePeer.DEGREEID);
		criteria.add(DegreeCoursePeer.COLLEGEID, collegeId);
		List temp;
		try{
			ArrayList<Curriculum> list = new ArrayList<Curriculum>();
			temp = CurriculumPeer.doSelect(criteria);
			ListIterator li = temp.listIterator();
			while(li.hasNext()){
				list.add(((Curriculum)li.next()));
			}
			return list;
		}catch (TorqueException e){
			e.printStackTrace();
			return null;
		}
	}
	
	
	/**
	 * Gets all the curricula
	 * 
	 * @return	All the curricula
	 */
	public List<Curriculum> getAllCurriculum(){
		Criteria criteria = new Criteria();
		criteria.addAscendingOrderByColumn(CURRICULUMCODE);
		try{
			return CurriculumPeer.doSelect(criteria);
		}
		catch (TorqueException e){
			e.printStackTrace();
			return null;
		}
	}
	
	
	/**
	 * Retrieves all the curricula that match the given keywords
	 * 
	 * @param curriculumCode - The code assigned to the course
	 * @param curriculumName - Tha name of the curriculum
	 * @param degreeId - The autogenerated id of the degree course
	 * @return
	 */
	public List<Curriculum> searchCurriculum(String curriculumCode, String curriculumName, Integer degreeId){
		Criteria criteria = new Criteria();
		criteria.setIgnoreCase(true);
		criteria.addAscendingOrderByColumn(CURRICULUMCODE);
		
		Criteria.Criterion c1 = criteria.getNewCriterion(CurriculumPeer.CURRICULUMCODE, "%" + curriculumCode + "%", Criteria.LIKE);
		c1 = c1.and(criteria.getNewCriterion(CurriculumPeer.CURRICULUMNAME, "%" + curriculumName + "%", Criteria.LIKE));
		
		if (degreeId != null)
			c1 = c1.and(criteria.getNewCriterion(CurriculumPeer.DEGREEID, degreeId, Criteria.EQUAL));
		
		criteria.add(c1);
		
		try{
			return CurriculumPeer.doSelect(criteria);
		}
		catch (TorqueException e){
			e.printStackTrace();
			return null;
		}
	}
	
	
	/**
	 * Imports the curricula
	 * 
	 * @param source - The line containing the curriculum's info
	 * @return - The college codes of the successfully imported curricula
	 */
	public List<String> importCurricula(String source){
		List<String> token = RegistTokenizer.tokenize(source);
		List<String> curriculumCodes = new Vector<String>();
		ListIterator<String> i = token.listIterator();
		while(i.hasNext()){
			String[] temp = i.next().split(",");
			if (temp.length != 4)
				return null;
			try{
			String curriculumCode = temp[0];
			String curriculumName = temp[1];
			Integer degreeId = new registnet.dao.DegreeCoursePeer().getDegreeCourseId(temp[2]);
			Integer unitId = new registnet.dao.UnitPeer().getUnitId(temp[3]);
			this.createNewCurriculum(curriculumCode, curriculumName, degreeId);
			curriculumCodes.add(curriculumCode);
			} catch (Exception e) {/* write to log */}
		}
		return curriculumCodes;
	}
	
	
	/**
	 * Determines if the curriculum has courses associated with it
	 * 
	 * @param curriculumId - The autogenerated id of the curriculum
	 * @return true if there is at least one course associated with it, false otherwise
	 */
	public boolean hasCurriculumCourse(Integer curriculumId){
		Criteria criteria = new Criteria();
		criteria.add(CurriculumCoursePeer.CURRICULUMID, curriculumId);
		
		try{
			CurriculumCoursePeer.doSelect(criteria).get(0);
			return true; 
		}catch (Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	
	/**
	 * Imports the curricula together with the curriculum courses associated with it
	 * 
	 * @param source
	 * @return
	 * @throws Exception
	 */
	public String importCurriculumSpecification(String source) throws Exception {
		
		List<String> token = RegistTokenizer.tokenize(source);
		ListIterator<String> i = token.listIterator();
		CurriculumCoursePeer ccp = new CurriculumCoursePeer();
		/*
		 * Insert into curriculum
		 * */
		String curriculumCode = null;
		String curriculumName;
		Integer curriculumId = null;
		Integer degreeId;
		
		Integer ahCount = 0;
		Integer mstCount = 0;
		Integer sspCount = 0;
		Integer electiveCount = 0;
		
		String[] temp = i.next().split(",");
		if(temp.length != 3)
			return null;
		try{
			curriculumCode = temp[0];
			curriculumName = temp[1];
			degreeId = new registnet.dao.DegreeCoursePeer().getDegreeCourseId(temp[2]);
			curriculumId = this.createNewCurriculum(curriculumCode, curriculumName, degreeId);
			if (curriculumId == null)
				return null;
		} catch (Exception e) {/* write to log */}
		
		
		
		/*
		 * Insert into curriculum_course
		 * */
		while(i.hasNext()){
			String[] str = i.next().split(",");
			if (str.length != 5)
				break;
			try{
				Integer courseId = new CoursePeer().getCourseId(str[0]);
				Integer semester = new Integer(resources.Constants.MAP_SEMESTER_KEY.get(str[1]));
				Integer year = new Integer(resources.Constants.MAP_YEAR_KEY.get(str[2]));
				String prerequisite = str[3];
				Integer ccid = ccp.createNewCurriculumCourse(curriculumId, courseId, semester, year, prerequisite);
				if (ccid == null){
					this.deleteCurriculum(curriculumId);
					return null;
				}
				else{
					if (str[0].contains("(AH)"))
						ahCount++;
					else if (str[0].contains("(MST)"))
						mstCount++;
					else if (str[0].contains("(SSP)"))
						sspCount++;
					else if (str[0].contains("ELECTIVE"))
						electiveCount++;
				}
			} catch (Exception e) {/* write to log */}
		}
		
		List<CurriculumCourse> curriculumCourses = new CurriculumCoursePeer().getCurriculumCourses(curriculumId);		
		HashMap<Integer, Integer> classification = new HashMap<Integer, Integer>();
		int totalUnits = 0;
		for(int x=0; x<curriculumCourses.size(); x++){
			CurriculumCourse course = curriculumCourses.get(x);
			if (!course.getCourse().getCourseCode().startsWith("NSTP")){
				if (!classification.containsKey(course.getYear())){
					totalUnits += course.getCourse().getUnits();
					classification.put(course.getYear(), course.getCourse().getUnits());
				}
				else{
					totalUnits += course.getCourse().getUnits();
					classification.remove(course.getYear());
					classification.put(course.getYear(), totalUnits);
				}
			}
		}
		
		CurriculumClassificationPeer curriculumClassificationPeer = new CurriculumClassificationPeer(); 
		Iterator<Integer> iter = classification.keySet().iterator();
		while(iter.hasNext()){
			 int year = iter.next();
			 int count = classification.get(year);
			 curriculumClassificationPeer.createNewCurriculumClassification(curriculumId, count, year);
		}
		
		List<CurriculumClassification> curriculumClassifications = curriculumClassificationPeer.getAllClassifications(curriculumId);
		for (int x=0; x<curriculumClassifications.size(); x++){
			CurriculumClassification curriculumClassification = curriculumClassifications.get(x);
		}
		
		this.updateCurriculum(curriculumId, totalUnits, ahCount, mstCount, sspCount, electiveCount);
		
		return curriculumCode;
	}
	
	public void setUser(String user, String logUnitCode){
		this.user = user;
		this.logUnitCode = logUnitCode;
	}
}
